package com.jiaofu.controller;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiaofu.config.ResultPage;
import com.jiaofu.pojo.PerInfo;
import com.jiaofu.pojo.Person;
import com.jiaofu.service.PersonService;
import com.jiaofu.vo.PersonVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/persons")
public class PersonController {

    @Autowired
    private PersonService personService;

    @RequestMapping("/page")
    @ResponseBody
    public ResultPage<Person> personPage(PersonVo personVo) {
        IPage<Person> page = new Page<>(personVo.getPage(), personVo.getLimit());
        QueryWrapper<Person> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(personVo.getUsername()),"username", personVo.getUsername());
        wrapper.eq(StringUtils.isNotBlank(personVo.getCreator()),"creator", personVo.getCreator());
        wrapper.ge(personVo.getStartTime() != null,"createdate", personVo.getStartTime());
        wrapper.le(personVo.getEndTime() != null,"createdate", personVo.getEndTime());
        IPage<Person> iPage = personService.page(page, wrapper);
        return new ResultPage<>(0, iPage.getRecords(), "success", iPage.getTotal());
    }

    @RequestMapping("/del")
    @ResponseBody
    public ResultPage<Person> del(String id) {
        personService.removeById(id);
        Long size = (long) personService.list().size();
        return new ResultPage<>(0, null,"success", size);
    }

    @RequestMapping("/info")
    @ResponseBody
    public ResultPage<PersonVo> info(int id) {
        List<PersonVo> people = personService.selectPerson(id);
        System.out.println(people);
        return new ResultPage<>(0, people,"success", 1L);
    }

}
